(function() {
    let url = decodeURI(location.search).slice(1).split('&');
    let newUrl = {};
    url.forEach(item => {
        let key = item.split('=')[0];
        let value = item.split('=')[1];
        newUrl[key] = value;
    });
    console.log(newUrl);

    //面包屑
    let position = `
    <a href="../html/index.html">首页</a> > <a href="../html/brandTitle.html">全部分类</a> > <a href="./brandlist.html?title=${newUrl.title}&brandtitleid=${newUrl.brandtitleid}">${newUrl.title}</a> >
    `;
    $('.position').html(position);
    let pageNumber = 0;
    //下拉框监听事件
    getDate();

    function getDate(index = 0) {
        index -= 0;
        fetch('http://chst.vip:1234/api/getbrandproductlist?brandtitleid=' + newUrl.brandtitleid + '&pagesize=4').then(body => body.json()).then(res => {
            let result = res.result;
            console.log(result);
            let liStr = '';
            result.forEach(async item => {
                let abc = await getDetails(item.productId);
                liStr += `
                    <li date-index="${item.productId}">
                        <a href="#">
                            <div class="img">
                                ${item.productImg}
                            </div>
                            <div class="info">
                                <h3>${item.productName}</h3>
                                <div class="price">${item.productPrice}</div>
                                <div class="other">
                                    <span>${item.productQuote}</span>
                                    <span>${item.productCom}</span>
                                </div>
                            </div>
                        </a>
                    </li>
                    <div class="review">
                    ${abc}
                    </div>
                    `
                $('.prolis').html(liStr);
            });

        });

        // 请求评论
        async function getDetails(index) {
            index = index - 0;
            let detailStr = ``;
            await fetch('http://chst.vip:1234/api/getproductcom?productid=' + index).then(body => body.json()).then(res => {
                let result = res.result;
                console.log(result);
                result.forEach(item => {
                    detailStr += `
                    <li>
                    <div class="title">
                        <span>${item.comName}</span>
                        <span>${item.comFrom}</span>
                        <span>${item.comTime}</span>
                        </div>
                        <p>
                        ${item.comContent}
                        </p>
                        </li>
                `
                });
            });
            return detailStr
        };
    };

    // 
    $('.prolis').on('click', 'li', function() {
        //  location.href = '../html/productlist.html?categoryid=' + newUrl.ategoryid + '&productid=' + $(this).attr('date-index')
        $(this).next().stop().slideToggle(200)

    })
})()